
function isEmpty(target,changeTag){
	if(target.val() == ''){
		changeTag.html('<font style="color:red">You can\'t leave this empty</font>');
		return true;
	}
	else{
		changeTag.html('');
		return false;
	}
}

function checkUsername(){
	if(!isEmpty($('#username'),$('#errorUsername'))){
		$.post("check_username.php",{username: $('#username').val()},function(data){
			if(data == 'false')
				$('#errorUsername').html('<font style="color:red">Someone already has that username. Try anothor?</font>');
			else
				$('#errorUsername').html('');
		});
	}
}

function checkPassword(){
	isEmpty($('#password'),$('#errorPassword'));
}

function checkConfirmPassword(){
	if(!isEmpty($('#confirmPassword'),$('#errorPassword2'))){
		if($('#password').val() != $('#confirmPassword').val())
			$('#errorPassword2').html('<font style="color:red">It is not same as password</font>');
		else
			$('#errorPassword2').html('');
	}
}

function checkSecurityQuestion(target,changeTag){
	if(!isEmpty(target,changeTag)){
		if(target.val() == '')
			changeTag.html('<font style="color:red">Question is empty</font>');
		else
			changeTag.html('');
	}
}

function checkSecurityAnswer(target,changeTag){
	isEmpty(target,changeTag);
}

$(document).ready(function(){
	$('#username').focusout(function(){
		checkUsername();
	});

	$('#password').focusout(function(){
		checkPassword();
	});	
	
	$('#confirmPassword').focusout(function(){
		checkConfirmPassword();
	});

	$('#securityQuestion1').focusout(function(){
		checkSecurityQuestion($(this),$('#errorSQ1'));
	});

	$('#securityAnswer1').focusout(function(){
		checkSecurityAnswer($(this),$('#errorSA1'));
	});

	$('#securityQuestion2').focusout(function(){
		checkSecurityQuestion($(this),$('#errorSQ2'));
	});

	$('#securityAnswer2').focusout(function(){
		checkSecurityAnswer($(this),$('#errorSA2'));
	});

	$('#agree').click(function(){
		if(this.checked)
			$('input[type="submit"]').removeAttr('disabled');
		else
			$('input[type="submit"]').attr('disabled','disabled');
	});

	
});